﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongThanhPham
{
    public partial class DanhSachSP : UserControl
    {
        public DanhSachSP()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            var tuNgay_gvSanPham = dtTuNgay_gvSanPham.Checked ? dtTuNgay_gvSanPham.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/01/" + DateTime.Now.Year;
            var denNgay_gvSanPham = dtDenNgay_gvSanPham.Checked ? dtDenNgay_gvSanPham.Value.ToString("MM/dd/yyyy") : DateTime.Now.Month + "/"
                + DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month) + "/" + DateTime.Now.Year;
            var table = DataAll.SelectAll("SanPhamHoanThien", "*", " WHERE (CONVERT(DATE, NgayLL, 0) >= CONVERT(DATE,'"
                + tuNgay_gvSanPham + "', 0) AND CONVERT(DATE, NgayLL, 0) <= CONVERT(DATE,'" + denNgay_gvSanPham + "' , 0))");
            gridSanPham.DataSource = table;
            SetPropertiesGrid();
        }

        public void SetPropertiesGrid()
        {
            gvSanPham.BestFitColumns();
            gvSanPham.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
            gvSanPham.OptionsSelection.EnableAppearanceFocusedCell = false;
            gvSanPham.OptionsSelection.MultiSelect = true;
            gvSanPham.OptionsView.ShowGroupPanel = false;
            gvSanPham.OptionsView.ColumnAutoWidth = false;
            gvSanPham.OptionsView.ShowFooter = true;
            gvSanPham.OptionsFind.AlwaysVisible = true;
            for (int i = 2; i < gvSanPham.Columns.Count; i++)
            {
                gvSanPham.Columns[i].OptionsColumn.AllowEdit = false;
            }
            gcXoa.Width = 40;
        }

        private void txtText_Click(object sender, EventArgs e)
        {
            var saveFile = new SaveFileDialog();
            saveFile.Filter = "Text Documents (*.txt)|*.txt|All files (*.*)|*.*";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                ExportText(saveFile.FileName);
            }
        }

        public void ExportText(string filepath)
        {
            var data = (DataView)gvSanPham.DataSource;
            StreamWriter str = File.AppendText(filepath);
            for (int i = 0; i < data.Count; i++)
            {
                if (!string.IsNullOrEmpty(data[i]["MaHang"].ToString()))
                {
                    str.WriteLine(data[i]["MaHang"].ToString() + data[i]["DuoiHang"] + "," + data[i]["SoLuong"]);
                }
            }
            str.Close();
            MessageBox.Show("Export file txt thành công!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dtTuNgay_gvSanPham_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dtDenNgay_gvSanPham_Leave(object sender, EventArgs e)
        {
            LoadData();
        }

        private void bntXoa_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var datacheck = DataAll.SelectAll("SanPhamTrongCuaHang", "IDSpHoanThien", "WHERE IDSpHoanThien="+gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "ID"));
            if (datacheck.Rows.Count <= 0)
            {
                if (gvSanPham.RowCount > 0)
                {
                    DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa sản phẩm.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dialogResult == DialogResult.Yes)
                    {
                        string message = DataAll.Update("PhongMau_SanPham", " SLConLai=SLConLai+" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "SoLuong") +
                            ", TrangThai =" + Convert.ToInt32(TrangThai.PhongMau_ThanhPham) + " WHERE IdSanPham =" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "IdSanPham"));
                        if (message.Equals("success"))
                        {
                            message = DataAll.Delete("SanPhamHoanThien", " ID=" + gvSanPham.GetRowCellValue(gvSanPham.FocusedRowHandle, "ID"));
                        }
                        MessageBox.Show(message.Equals("success") ? "Xóa thành công" : message, "Message",
                                                        MessageBoxButtons.OK,
                                                        message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                        LoadData();
                    }
                }
            }
            else
            {
                MessageBox.Show("Sản phẩm này đã được chia cho cửa hàng. Không thể xóa","Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}
